<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use think\Db;

class GoodsModel extends Model
{
    // 确定链接表名
    protected $table = 'snake_goods';

    /**
     * 查询产品
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getGoodsByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的产品数量
     * @param $where
     */
    public function getAllGoods($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 添加产品
     * @param $param
     */
    public function addGoods($param)
    {
        Db::startTrans();
        try{
            $re = getArr($param);
            $result = $this->insertGetId($re['param']);
            if(false === $result){
                // 验证失败 输出错误信息
                throw new \Exception('添加失败');
            }else{
                if (!empty($re['arr1'])){
                    foreach ($re['arr1'] as $k => $v){
                        $re['arr1'][$k]['goods_id'] = $result;
                    }
                    $re = (new Shuxing())->saveAll($re['arr1']);
                    if (!$re){
                        throw new \Exception('添加失败');
                    }
                }
                return msg(1, url('Goods/index'), '添加成功');
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑产品信息
     * @param $param
     */
    public function editGoods($param)
    {
//        Db::startTrans();
        try{
            $re = getarrs($param);
            $result = $this->save($re['param'], ['id' => $re['param']['id']]);
            if(false === $result){
                // 验证失败 输出错误信息
                throw new \Exception('添加失败');
            }else{
                $shuxin = db('shuxing')->where('goods_id',$re['param']['id'])->field('id')->select();

                $shuxin1 = array_column($shuxin,'id');
                if (!empty($re['arr1'])){


                    foreach ($re['arr1'] as $k => $v){
                        if (isset($v['id'])){
                            if (in_array($v['id'],$shuxin1)){
                                $a = array_flip($shuxin1);
                                unset($shuxin1[$a[$v['id']]]);
                                db('shuxing')->where('id',$v['id'])->update(['name'=>$v['name'],'price'=>$v['price'],'goods_id'=>$re['param']['id']]);
                            }

                        }else{
                            $re['arr1'][$k]['goods_id'] = $re['param']['id'];
                            $ee = db('shuxing')->insertGetId($re['arr1'][$k]);
                        }

                    }

                    foreach ($shuxin1 as $v){
//                        db('shuxing')->where('id',$v)->update(['status'=>0]);
                        db('shuxing')->where('id',$v)->delete();
                    }

                }else{
                    foreach ($shuxin1 as $v){
//                        db('shuxing')->where('id',$v)->update(['status'=>0]);
                        db('shuxing')->where('id',$v)->delete();
                    }
                }

                return msg(1, url('Goods/index'), '编辑成功');
            }
//            Db::commit();
        }catch(\Exception $e){
//            Db::rollback();
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据产品的id 获取产品的信息
     * @param $id
     */
    public function getOneGoods($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 删除产品
     * @param $id
     */
    public function delGoods($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除产品成功');

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    public function getOnTimeAttr($v){
        return date('Y-m-d H:i:s',$v);
    }

    public function getCatIdAttr($v){
      $data =  db('fenlei')->where('id',$v)->field('name')->find();
        return $data['name'];
    }
}
